home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / AUTOEXCL.PAK / DRVEXCEL.CPP next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  146 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1995, 1996 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.0  $
  6. //
  7. // Simple controller driving Excel [Using TypeLibrary from Office 95 CD]
  8. //----------------------------------------------------------------------------
  9. #include <ocf/pch.h>
  10. #include "xl5en32.hxx"
  11.  
  12. HINSTANCE hInstance = 0;
  13.  
  14. void
  15. PlayWithActiveSheet(Workbook& workbook)
  16. {
  17.   // Bind to sheet of active workbook
  18.   //
  19.   Worksheet worksheet;
  20.   worksheet.Bind(LPDISPATCH(workbook.GetActiveSheet()));
  21.  
  22.   // Get range in worksheet
  23.   //
  24.   if (worksheet.IsBound()) {
  25.     Range range;
  26.     range.Bind(LPDISPATCH(worksheet.Range(string("A1"), string("H8"))));
  27.  
  28.     // Throw some random values in there - heck, it's free!
  29.     //
  30.     range.SetFormula(string("=rand()"));
  31.   }
  32. }
  33.  
  34. void
  35. CreateAWorkbook(Workbooks& workbooks)
  36. {
  37.   // Create new active workbook
  38.   //
  39.   Workbook workbook;
  40.   workbook.Bind(LPDISPATCH(workbooks.Add(TNoArg())));
  41.  
  42.   // Pass workbook object for more automation
  43.   //
  44.   PlayWithActiveSheet(workbook);
  45.  
  46.   // Save new document to current path
  47.   //
  48.   char path[_MAX_PATH];
  49.   ::GetModuleFileName(hInstance, path, sizeof(path));
  50.  
  51.   // NOTE: Cheap way to strip extension from current app's path - Argh!
  52.   //
  53.   TNoArg _x;
  54.   char *p = strstr(path, ".EXE");
  55.   if (p) *p = 0;
  56.   workbook.SaveAs(string(path), _x, _x, _x, _x, _x, _x, _x);
  57. }
  58.  
  59. void
  60. RetrieveWorkbooksObject(Application& xcl)
  61. {
  62.   // Have a 'Workbooks' object bind to workbook collection
  63.   //
  64.   Workbooks workbooks;
  65.   workbooks.Bind(LPDISPATCH(xcl.Workbooks()));
  66.  
  67.   // Have some more fun
  68.   //
  69.   CreateAWorkbook(workbooks);
  70. }
  71.  
  72. //
  73. // Routine that jumps start the automation sample by launching EXCEL,
  74. // making it visible...
  75. //
  76. void
  77. AutomateExcel()
  78. {
  79.   // Instance of proxy object representing EXCEL v7.0 application
  80.   //
  81.   Application xcl;
  82.  
  83.   try {
  84.  
  85.     // Bind to application
  86.     //
  87.     xcl.Bind("Excel.Application.5");
  88.  
  89.     // Talk to application
  90.     //
  91.     try {
  92.  
  93.       // Make Excel visible
  94.       //
  95.       xcl.SetVisible(true);
  96.  
  97.       // Go have some fun...
  98.       //
  99.       RetrieveWorkbooksObject(xcl);
  100.  
  101.       // Hide Excel
  102.       //
  103.       xcl.SetVisible(false);
  104.     }
  105.  
  106.     // Failure during Automation exchange with application
  107.     //
  108.     catch (xmsg& msg) {
  109.       MessageBox(0, msg.why().c_str(), "Exception Info", MB_OK);
  110.     }
  111.  
  112.     // Unload application - NOTE: Since we're invoking 'Quit' we need
  113.     // to unbind to prevent a 'Release' call in the destructor of the
  114.     // proxy object.
  115.     //
  116.     if (xcl.IsBound()) {
  117.       xcl.Quit();
  118.       xcl.Unbind(false);
  119.     }
  120.   }
  121.  
  122.   // Following most likely indicates a failure to bind/quit
  123.   //
  124.   catch (xmsg& msg) {
  125.     MessageBox(0, msg.why().c_str(), "Exception Info", MB_OK);
  126.   }
  127. }
  128.  
  129. //
  130. //
  131. //
  132. int PASCAL
  133. WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance,
  134.         LPSTR cmdLine, int cmdShow)
  135. {
  136.   try {
  137.     hInstance= hInst;
  138.     TOleInit initOle;
  139.     AutomateExcel();
  140.   }
  141.   catch (xmsg& x) {
  142.     MessageBox(0, x.why().c_str(), "Exception Info", MB_OK|MB_TASKMODAL);
  143.   }
  144.   return 0;
  145. }
  146.